function out = BOHitOrMiss2D(im,method )
%%  BOHitOrMiss - end and triple points detection
%   
%   REFERENCE:
%       Cecilia Di Ruberto, 
%       Recognition of shapes by attributed skeletal graphs, 
%       Pattern Recognition 37, 21-31, 2004
%   
%   HELP:                                       
%       Matlab's interpretation of the algorithm
%       1 ->  1; 0 -> -1; * ->  0;
%
%   INPUT:
%       im      - binary image.
%       method  - 'end' or 'triple'.
%
%   OUTPUT:
%       out     - image with detected points
%
%   USAGE:
%        % Skeleton
%        sk = bwmorph(im, 'thin', inf);
%        % Hit or Miss
%        out1 = BOHitOrMiss(sk, 'end');
%        out2 = BOHitOrMiss(sk, 'triple');
% 
%   AUTHOR:
%       Boguslaw Obara, http://boguslawobara.net/
%
%   VERSION:
%       0.1 - 16/07/2008 First implementation

%% SE - endpoints 
if strcmp(method, 'end')
se(:,:,1) = [   -1  1 -1   ;...
                -1  1 -1   ;...
                -1 -1 -1   ];
            
se(:,:,2) = [   -1 -1  1   ;...
                -1  1 -1   ;...
                -1 -1 -1   ];
            
se(:,:,3) = [   -1 -1 -1   ;...
                -1  1  1   ;...
                -1 -1 -1   ];
            
se(:,:,4) = [   -1 -1 -1   ;...
                -1  1 -1   ;...
                -1 -1  1   ];
            
se(:,:,5) = [   -1 -1 -1   ;...
                -1  1 -1   ;...
                -1  1 -1   ];
            
se(:,:,6) = [   -1 -1 -1   ;...
                -1  1 -1   ;...
                 1 -1 -1   ];
            
se(:,:,7) = [   -1 -1 -1   ;...
                 1  1 -1   ;...
                -1 -1 -1   ];
            
se(:,:,8) = [    1 -1 -1   ;...
                -1  1 -1   ;...
                -1 -1 -1   ];
%% SE - triple points (junctions)
elseif strcmp(method, 'triple')
se(:,:,1) = [   -1  1 -1   ;...
                 1  1  1   ;...
                -1 -1 -1   ];

se(:,:,2) = [    1 -1  1   ;...
                -1  1 -1   ;...
                -1 -1  1   ];

se(:,:,3) = [   -1  1 -1   ;...
                -1  1  1   ;...
                -1  1 -1   ];

se(:,:,4) = [   -1 -1  1   ;...
                -1  1 -1   ;...
                 1 -1  1   ];

se(:,:,5) = [   -1 -1 -1   ;...
                 1  1  1   ;...
                -1  1 -1   ];

se(:,:,6) = [    1 -1 -1   ;...
                -1  1 -1   ;...
                 1 -1  1   ];

se(:,:,7) = [   -1  1 -1   ;...
                 1  1 -1   ;...
                -1  1 -1   ];

se(:,:,8) = [    1 -1  1   ;...
                -1  1 -1   ;...
                 1 -1 -1   ];

se(:,:,9) = [   -1  1 -1   ;...
                -1  1  1   ;...
                 1 -1 -1   ];

se(:,:,10)= [   -1 -1  1   ;...
                 1  1 -1   ;...
                -1 -1  1   ];

se(:,:,11)= [    1 -1 -1   ;...
                -1  1  1   ;...
                -1  1 -1   ];

se(:,:,12)= [   -1  1 -1   ;...
                -1  1 -1   ;...
                 1 -1  1   ];

se(:,:,13)= [   -1 -1  1   ;...
                 1  1 -1   ;...
                -1  1 -1   ];

se(:,:,14)= [    1 -1 -1   ;...
                -1  1  1   ;...
                 1 -1 -1   ];

se(:,:,15)= [   -1  1 -1   ;...
                 1  1 -1   ;...
                -1 -1  1   ];

se(:,:,16)= [    1 -1  1   ;...
                -1  1 -1   ;...
                -1  1 -1   ];
end
%%  Hit or Miss
out = zeros(size(im));
if strcmp(method, 'end') || strcmp(method, 'triple')
    for i=1:size(se,3)
        hom = bwhitmiss(im, se(:,:,i));
        out = max(out, hom);
    end
end
%% 
end